package com.amc.jms;

import java.util.Map;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class Producer{
	private static ObjectMapper mapper = new ObjectMapper();
	public static void send(Map<String, String> map)throws JMSException, JsonProcessingException {
		ConnectionFactory cf = new ActiveMQConnectionFactory("tcp://localhost:61616");
		Connection conn = cf.createConnection();
	
		//Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
		Session session = conn.createSession(true, Session.AUTO_ACKNOWLEDGE);//事务
		Destination quenue = new ActiveMQQueue("merchant-queue");
		MessageProducer producer = session.createProducer(quenue);
		conn.start();
	//	for(int i = 0; i < 100; i++){
			TextMessage tm = session.createTextMessage(mapper.writeValueAsString(map));
			producer.send(tm);
			System.out.println("拉黑："+map);
		//}
		//提交事务
		session.commit();
		
		session.close();
		producer.close();
		conn.close();
		
	}
}
